package cn.jinjimi.app.reader.holder;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baiiu.filter.util.UIUtil;
import cn.jinjimi.app.R;
import cn.jinjimi.app.reader.bean.ArticleFourViewData;
import cn.jinjimi.app.main.widget.CommonRV.BaseHolder;
import cn.jinjimi.app.main.widget.CommonRV.IActionListener;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.main.widget.FrescoImageView.MaskPostProcessor;

/**
 * Created by lforxeverc on 2017/4/14.
 */

public class ArticleFourHolder extends BaseHolder<ArticleFourViewData> {
    private FrescoImageView ivCover;
    private ImageView ivTag;
    private RelativeLayout rlShare;
    private TextView tvTitle;
    private View pointStart, pointEnd, vDivider;
    private int dp5, dp15;

    public ArticleFourHolder(View v, Context context, int type) {
        super(v, context, type);
        dp5 = UIUtil.dp(context, 5);
        dp15 = UIUtil.dp(context, 15);
    }

    @Override
    protected void initView() {
        ivCover = (FrescoImageView) itemView.findViewById(R.id.iv_cover);
        ivTag = (ImageView) itemView.findViewById(R.id.iv_tag);
        rlShare = (RelativeLayout) itemView.findViewById(R.id.rl_share);
        tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        pointStart = itemView.findViewById(R.id.v_point_start);
        pointEnd = itemView.findViewById(R.id.v_point_end);
        vDivider = itemView.findViewById(R.id.v_divider);
        ivCover.setPostProcessor(new MaskPostProcessor(getContext(), R.drawable.article_pic_mask));
    }

    @Override
    public void setViews(final int position, final ArticleFourViewData data, final IActionListener mListener) {
        ivCover.loadView(data.getCoverUrl());
        tvTitle.setText(data.getTitle());
        if (data.getCurrentPosition() == 0) {
            ivTag.setVisibility(View.VISIBLE);
        } else if (data.getCurrentPosition() == 1) {
            ivTag.setVisibility(View.VISIBLE);
        } else {
            ivTag.setVisibility(View.GONE);
        }
        int curPosition = data.getCurrentPosition();
        boolean isLeftItem = curPosition % 2 == 0;
        pointStart.getLayoutParams().width = isLeftItem ? dp15 : dp5;
        pointEnd.getLayoutParams().width = isLeftItem ? dp5 : dp15;
        if (curPosition == 2) {
            vDivider.setVisibility(View.VISIBLE);
            ViewGroup.MarginLayoutParams margin = (ViewGroup.MarginLayoutParams) vDivider.getLayoutParams();
            margin.leftMargin = dp15;
            vDivider.setLayoutParams(margin);
        } else if (curPosition == 3) {
            vDivider.setVisibility(View.VISIBLE);
            ViewGroup.MarginLayoutParams margin = (ViewGroup.MarginLayoutParams) vDivider.getLayoutParams();
            margin.rightMargin = dp15;
            vDivider.setLayoutParams(margin);
        } else {
            vDivider.setVisibility(View.GONE);
        }
    }
}
